package es.udc.fic.hamNeggs.pojo;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Entity
@Table(name = "hamneggs.meetings")
public class Meeting {
    @Id
    @GeneratedValue
    @Column(name = "mtg_id")
    private Long                id;

    @Column(name = "mtg_type")
    @Enumerated(EnumType.STRING)
    private MeetingType         type;

    @Column(name = "mtg_date")
    private Date                date;

    @ManyToOne
    @JoinColumn(name = "prj_id")
    private Project             project;

    @ManyToOne
    @JoinColumn(name = "usr_id")
    private User                meetingLeader;

    @ManyToMany(cascade = { CascadeType.PERSIST })
    @JoinTable(name = "hamneggs.goesto",
            joinColumns = { @JoinColumn(name = "mtg_id") },
            inverseJoinColumns = { @JoinColumn(name = "usr_id") })
    private Set<User>           attendees;

    @OneToMany(mappedBy = "meeting")
    private Set<MeetingComment> comments;

    public Meeting() {

    }

    public MeetingType getType() {
        return type;
    }

    public void setType(MeetingType type) {
        this.type = type;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Long getId() {
        return id;
    }

    public Project getProject() {
        return project;
    }

    public User getMeetingLeader() {
        return meetingLeader;
    }

    public List<User> getAttendees() {
        List<User> lattendees = new ArrayList<User>();
        lattendees.addAll(attendees);
        Collections.sort(lattendees,
                new Comparator<User>() {
                    public int compare(User o1, User o2) {
                        return o1.getId().intValue() - o2.getId().intValue();
                    }
                });
        return lattendees;
    }

    public void setAttendee(User user) {
        if (attendees == null)
            attendees = new HashSet<User>();
        attendees.add(user);
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public void setMeetingLeader(User meetingLeader) {
        this.meetingLeader = meetingLeader;
        setAttendee(meetingLeader);
    }

    public enum MeetingType {
        DailyScrum("DailyScrum"),
        BacklogGrooming("BacklogGrooming"),
        ScrumOfScrums("ScrumOfScrums"),
        SprintPlanning("SprintPlanning"),
        SprintReview("SprintReview"),
        SprintRetrospective("SprintRetrospective");

        private final String type;

        private MeetingType(final String type) {
            this.type = type;
        }

        public String getString() {
            return type;
        }
    }

    public void clearAttendees() {
        attendees.clear();
    }

    public void setOneComment(MeetingComment cmt) {
        if (comments == null)
            comments = new HashSet<MeetingComment>();
        comments.add(cmt);
    }

    public Set<MeetingComment> getComments() {
        return comments;
    }

    public void setComments(final Set<MeetingComment> comments) {
        this.comments = comments;
    }

    public void setAttendees(final Set<User> attendees) {
        this.attendees = attendees;
    }

}
